package com.mobile.challenge.ui.report.unit.manager.weekmonth

import android.graphics.Color
import android.os.Bundle
import androidx.lifecycle.Observer
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.formatter.ValueFormatter
import com.mobile.challenge.R
import com.mobile.challenge.databinding.FragmentManagerLineBinding
import com.mobile.challenge.ui.report.adapter.IRefresh
import com.mobile.frame.mvvmframe.base.BaseFragment
import java.util.*

/**
 * @Author Touge
 * @Date 2020/9/16 21:51
 * @Description
 */
class ManagerLineFragment : BaseFragment<ManagerLineViewModel, FragmentManagerLineBinding>(), IRefresh {

    private var periodType = 0

    companion object {
        const val PERIOD_TYPE = "period_type"
        fun newInstance(periodType: Int): ManagerLineFragment {
            val args = Bundle()
            args.putInt(PERIOD_TYPE, periodType)
            val fragment = ManagerLineFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            periodType = it.getInt(PERIOD_TYPE, 0)
        }
    }


    override fun getLayoutId(): Int {
        return R.layout.fragment_manager_line
    }

    override fun initData(savedInstanceState: Bundle?) {
        initBarChart()
        mViewModel.lineData.observe(this, Observer { data ->
            mBinding.lineChart.data = data
            mBinding.lineChart.animateXY(1000, 1000)
        })
        mViewModel.generateData(periodType, Calendar.getInstance())
    }

    private fun initBarChart() {
        with(mBinding.lineChart) {
            //是否展示网格线
            setDrawGridBackground(false)
            //是否显示边界
            setDrawBorders(true)
            //是否可以拖动
            isDragEnabled = true
            //是否有触摸事件
            setTouchEnabled(true)
            //设置XY轴动画效果
            animateY(2500)
            animateX(1500)

            //X轴设置显示位置在底部
            xAxis.position = XAxis.XAxisPosition.BOTTOM
            xAxis.axisMinimum = 0f
            xAxis.granularity = 1f

            xAxis.labelRotationAngle = 90F
            xAxis.valueFormatter = object : ValueFormatter() {

                override fun getFormattedValue(value: Float): String {
                    if (mViewModel != null) {
                        return mViewModel.getDataStr(value.toInt())
                    }
                    return ""
                }
            }

            //保证Y轴从0开始，不然会上移一点
            axisLeft.axisMinimum = 0f
            axisRight.axisMinimum = 0f

            // 图例
            legend.setForm(Legend.LegendForm.LINE);
            legend.setTextSize(12f);
            //显示位置 左下方
            legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
            legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
            legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
            //是否绘制在图表里面
            legend.setDrawInside(false);

            setBackgroundColor(Color.WHITE)
            //是否显示边界
            setDrawBorders(false)

            //是否展示网格线
            setDrawGridBackground(false);

            xAxis.setDrawGridLines(false);
            axisRight.setDrawGridLines(false);
            axisLeft.setDrawGridLines(true);

            // 虚线
            axisLeft.enableGridDashedLine(10f, 10f, 0f);

            // 禁用右侧 Y
            axisRight.isEnabled = false;
        }
    }

    override fun refresh(calendar: Calendar, period: Int) {
        if (mViewModel != null) {
            mViewModel.generateData(periodType, calendar)
        }
    }
}